package djboom.materials {
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.core.DataCore;
	import djboom.materials.display.MainPane;
	import djboom.materials.events.BitmapDataCoreEvent;
	import djboom.materials.events.MainPaneEvent;

	import com.greensock.plugins.MotionBlurPlugin;
	import com.greensock.plugins.ScrollRectPlugin;
	import com.greensock.plugins.TransformAroundCenterPlugin;
	import com.greensock.plugins.TransformAroundPointPlugin;
	import com.greensock.plugins.TweenPlugin;

	import org.casalib.util.AlignUtil;
	import org.casalib.util.StageReference;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.system.System;

	/**
	 * Материаловедение.
	 * Комплексная виртуальная лабораторная работа.
	 * "Основные свойства строительных материалов".
	 * <br>
	 * 
	 * @author DJ BooM
	 * @since 06.04.2010
	 * 
	 */

	[SWF(backgroundColor="#1e1e1e", frameRate="30", width="1024", height="768")]

	public class Work extends Sprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		/**
		 * Версия лабораторной работы.
		 * 
		 */

		public static const WIDTH:int = 1024;
		public static const HEIGHT:int = 768;
		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		/**
		 * Импорт шрифтов.
		 * 
		 */

		[Embed(source="../../../assets/fonts/MyriadPro-Cond.otf", fontFamily="Myriad Pro Cond")]
		public var MyriadProCondFont:Class;

		[Embed(source="../../../assets/fonts/MyriadPro-CondIt.otf", fontFamily="Myriad Pro Cond", fontStyle="italic")]
		public var MyriadProCondItalicFont:Class;

		[Embed(source="../../../assets/fonts/MyriadPro-BoldCond.otf", fontFamily="Myriad Pro Cond", fontWeight="bold")]
		public var MyriadProCondBoldFont:Class;

		[Embed(source="../../../assets/fonts/MyriadPro-BoldCondIt.otf", fontFamily="Myriad Pro Cond", fontStyle="italic", fontWeight="bold")]
		public var MyriadProCondBoldItalicFont:Class;

		[Embed(source="../../../assets/fonts/SpecialSymbols.ttf", fontFamily="SpecialSymbols")]
		public var SpecialSymbolsFont:Class;

		[Embed(source="../../../assets/fonts/Crystal.ttf", fontFamily="Crystal")]
		public var CrystalFont:Class;

		private var _mainPane:MainPane;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Work() {
			addEventListener(Event.ADDED_TO_STAGE, initWork);			
		}

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function initWork(event:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, initWork);
			
			/**
			 * Настройки сцены.
			 * 
			 */

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
            
			StageReference.setStage(stage);
            
			/**
			 * Инициализируем фильтры.
			 * 
			 */

			TweenPlugin.activate([ScrollRectPlugin]);
			TweenPlugin.activate([MotionBlurPlugin]);
			TweenPlugin.activate([TransformAroundCenterPlugin]);
			TweenPlugin.activate([TransformAroundPointPlugin]);
             
			/**
			 * Данные.
			 * 
			 */

			DataCore.init();
            
			/**
			 * Загрузка элементов интерфейса.
			 * 
			 */

			var bitmapDataCore:BitmapDataCore = new BitmapDataCore();           
			bitmapDataCore.addEventListener(BitmapDataCoreEvent.COMPLETE, interfaceСomplete);
			
			
		}    

		private function interfaceСomplete(event:BitmapDataCoreEvent):void {
			_mainPane = new MainPane();
			_mainPane.addEventListener(MainPaneEvent.EXIT, exitWork);
			addChild(_mainPane);

			stage.addEventListener(Event.RESIZE, resizeStage);
            
			resizeStage();
		}

		private function resizeStage(event:Event = null):void {
			AlignUtil.alignMiddleCenter(_mainPane, new Rectangle(0, 0, stage.stageWidth, stage.stageHeight));
		}

		private function exitWork(event:MainPaneEvent):void {
			System.exit(0);
		}
	}
}